package yi.wuhenbot.Service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import yi.wuhenbot.Utils.QQBotUtil;
import yi.wuhenbot.dao.OrgUserServiceMapper;
import yi.wuhenbot.entity.OrgUser;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 用户签到服务
 * @author wuhen
 */
@Service("orgUserService")
public class OrgUserService {

    @Autowired
    private OrgUserServiceMapper orgUserServiceMapper;
    @Autowired
    private QQBotUtil qqBotUtil;

    String content;
    /**
     * 用户签到
     * @param userOpenid 用户id
     */
    public String signIn(String typeId,String type,String userOpenid) {
        System.out.println("开始处理签到功能");
        boolean isSignInToday = orgUserServiceMapper.isUserExist(userOpenid);

        System.out.println("用户是否存在："+isSignInToday);
        // 签到，先判断用户是否存在
        if (isSignInToday){
            System.out.println("用户存在，开始签到");
            // 先判断用户是否签到过
            // 确保与数据库存储格式匹配
            String today = new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(new Date());
            if (orgUserServiceMapper.isSignInToday(userOpenid, today)) {
                content="用户已签到，不能重复签到";
            }else {
                // 获取用户积分
                int points1 = orgUserServiceMapper.getPoints1(userOpenid);
                // 签到积分随机增加2~4
                int points1Add = (int) (Math.random() * 3 + 2);
                int points =   points1+points1Add;
                // 签到时间设置为当前时间
                String sign_in_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                System.out.println("签到时间："+sign_in_time);
                orgUserServiceMapper.setPoints1(userOpenid,points,sign_in_time);
                content="签到成功！\n"+
                        "签到积分+"+points1Add+"\n"+
                        "当前积分为："+points;
            }
        }else {
            System.out.println("用户不存在，开始自动注册");
            // 注册用户
            userEnroll(typeId,type,userOpenid);
            // 获取用户积分
            int points1 = orgUserServiceMapper.getPoints1(userOpenid);
            // 签到积分随机增加2~4
            int points1Add = (int) (Math.random() * 3 + 2);
            int points =   points1+points1Add;
            // 签到时间设置为当前时间
            String sign_in_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            System.out.println("签到时间："+sign_in_time);
            orgUserServiceMapper.setPoints1(userOpenid,points,sign_in_time);
            content="签到成功！\n"+
                    "签到积分+"+points1Add+"\n"+
                    "当前积分为："+points;
            content=content+"\n"+"用户未注册,已自动注册，改名请发送 ‘改名：昵称’ 即可修改昵称";
        }
        return content;
    }

    /**
     * 用户注册服务
     * @param userOpenid 用户openid
     * @return 注册结果
     */
    public String userEnroll(String typeId,String type,String userOpenid) {

        // 注册用户
        // 先判断用户是否存在
        if (orgUserServiceMapper.isUserExist(userOpenid)){
            content="用户已注册";
        }else {
            // 设置用户注册时间
            String registration_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            // 设置签到时间为1999年1月1日
            String sign_in_time="1999-01-01 00:00:00";

            // 插入用户
            orgUserServiceMapper.insertUser(userOpenid,registration_time,sign_in_time);
            content="用户注册成功，发送 ‘改名：昵称’ 即可修改昵称";
        }
        return content;
    }
    /**
     * 判断用户是否存在
     * @param userOpenid 用户openid
     * @return true存在 false不存在
     */
    public boolean isUserExist(String userOpenid) {
        return orgUserServiceMapper.isUserExist(userOpenid);
    }
    /**
     * 修改用户昵称
     * @param userOpenid 用户openid
     * @param nickname 昵称
     */
    public String updateNickname(String typeId,String type,String userOpenid, String nickname) {
        // 修改用户昵称
        // 移出改名：字符
        nickname = nickname.replaceAll("改名：", "");
        nickname = nickname.replaceAll("/改名 ", "");
        nickname = nickname.replaceAll("设置名称：", "");
        nickname = nickname.replaceAll("改名 ", "");
        // 移出空格
        nickname = nickname.trim();
        orgUserServiceMapper.updateNickname(userOpenid,nickname);
        content="用户昵称修改成功，昵称为【"+nickname+"】";
        return content;
    }
    /**
     * 获取用户签到积分
     * @param userOpenid 用户openid
     * @return 积分1
     */
    public int getPoints1(String userOpenid) {
        return orgUserServiceMapper.getPoints1(userOpenid);
    }
    /**
     * 获取用户信息
     * @param userOpenid 用户openid
     * @return 用户信息
     */
    public String getUserInfo(String typeId,String type,String userOpenid) {
        // 检查用户是否存在
        if (!orgUserServiceMapper.isUserExist(userOpenid)) {
            content="用户不存在，请先注册";
            return content;
        }else {
        OrgUser orgUser = orgUserServiceMapper.getUserInfo(userOpenid);
        content="用户信息：\n" +
                // 如果昵称为null则返回用户id
                "用户昵称：" + (orgUser.getNickname() == null ? orgUser.getUser_id() : orgUser.getNickname()) + "\n" +
                //格式化时间：移除年份时间格式为分钟
                "注册时间：" + orgUser.getRegistration_time().substring(5,16) + "\n" +
                "上次签到时间：" + orgUser.getSign_in_time().substring(5,16)+ "\n" +
                "\uD83D\uDCAF积分:" + orgUser.getPoints1() + "\n" +
                "修改昵称请发送：改名 昵称";
//                +"\uD83E\uDE99金币:" + orgUser.getPoints2() + "  " +
//                "\uD83D\uDC44魅力:" + orgUser.getPoints3() ;
        return content;
        }
    }
    /**
     * 判断用户是否为管理员
     * @param userOpenid 用户openid
     * @return 是否为管理员 true为管理员 false为普通用户
     */
    public boolean isAdmin(String typeId,String type,String userOpenid) {
        return orgUserServiceMapper.isAdmin(userOpenid);
    }

    /**
     * 获取用户连续对话状态
     * @param userOpenid 用户openid
     * @return 连续对话状态 0未开启对话 1已开启对话
     */
    public int getonline_status(String typeId,String type,String userOpenid) {
        // 检查用户是否存在
        if (!orgUserServiceMapper.isUserExist(userOpenid)) {
            // 创建用户
            orgUserServiceMapper.insertUser(userOpenid,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),"1999-01-01 00:00:00");
            // 初始化用户连续对话状态为0
            orgUserServiceMapper.updateonline_status(userOpenid,0);
            return 0;
        }
        return orgUserServiceMapper.getonline_status(userOpenid);
    }
    /**
     * 更新用户连续对话状态
     * @param userOpenid 用户openid
     * @param online_status 连续对话状态 0未开启对话 1已开启对话
     */
    public void updateonline_status(String typeId,String type,String userOpenid, int online_status) {

        orgUserServiceMapper.updateonline_status(userOpenid,online_status);
    }
    /**
     * 获取本群id
     * @return 本群id
     */
    public String getGroupId(String typeId,String type,String userOpenid) {
        // 检查用户是否存在
        if (!orgUserServiceMapper.isUserExist(userOpenid)) {
            content="用户不存在，请先注册";
            return content;
            //检查type是否为群
        }else if (type.equals("GROUP")) {
            return typeId;
        }else {
            content="用户不在群聊中，请在群聊中使用该命令";
            return content;
        }


    }
    /**
     * 获取用户id
     * @return 用户id
     */
    public String getUserid(String userOpenid) {
        userOpenid = "\n"+userOpenid;
        return userOpenid;
    }    /**
     * 获取用户id
     * @return 用户id
     */
    public String getUserid(String typeId,String type,String userOpenid) {
        userOpenid = "\n"+userOpenid;
        return userOpenid;
    }
    /**
     * 获取用户头像-命令我的头像
     * @param userOpenid 用户openid
     * @return 头像url
     */
    public String getUserAvatar(String typeId,String type,String userOpenid) {
        // 检查用户是否存在
        if (!orgUserServiceMapper.isUserExist(userOpenid)) {
            content="用户不存在，请先注册";
            return content;
        }
        // 获取用户头像
        String avatar = qqBotUtil.getUserAvatar(userOpenid);
        if (avatar == null) {
            content="用户未设置头像";
            return content;
        }
        content="用户头像："+avatar;
        return content;
    }
    /**
     * 判断用户是否已开启对话
     * @param userOpenid 用户openid
     * @return true已开启 false未开启
     */
    public boolean isUserOnline(String typeId,String type,String userOpenid) {
        return orgUserServiceMapper.getonline_status(userOpenid) == 1;
    }
}
